#include <iostream>
#include "Trie.hpp"
#include "mini_test.h"
#include "Conj.h"

template<typename T>
string to_s(const Conj<T>& r) {

  ostringstream os;
  os << r;
  return os.str();

}


void check_trie_clona_autor() {

  Trie c;

  char* p = new char[4];

  p[0] = 'A';
  p[1] = 'B';
  p[2] = 'C';
  p[3] = '\0';

  c.agregarAutor(p);
  delete p;

  p = new char[4];

  p[0] = 'a';
  p[1] = 'b';
  p[2] = 'c';
  p[3] = '\0';

  c.agregarAutor(p);
  delete p;

  p = new char[6];

  p[0] = 'a';
  p[1] = 'b';
  p[2] = 'c';
  p[3] = 'd';
  p[4] = 'e';
  p[5] = '\0';

  c.agregarAutor(p);
  delete p;

  ASSERT_EQ(c.estaDefinido("ABC"), true);
  ASSERT_EQ(c.estaDefinido("abc"), true);
  ASSERT_EQ(c.estaDefinido("abcde"), true);

}

void check_trie_claves(){

	Trie tr;
	Conj<string> c;

	tr.agregarAutor("Jimmy Hendrix");
	c.Agregar("Jimmy Hendrix");

	tr.agregarTema("Hey Joe", c);
	tr.agregarTema("Voodoo Child", c);
	tr.agregarTema("Purple Haze", c);

	ASSERT_EQ(to_s(tr.obtenerClave("Jimmy Hendrix")), "{Purple Haze,Voodoo Child,Hey Joe}");

	tr.agregarAutor("Eric Clapton");
	c.Agregar("Eric Clapton");

	tr.agregarTema("Little Wing", c);
	ASSERT_EQ(to_s(tr.obtenerClave("Jimmy Hendrix")), "{Little Wing,Purple Haze,Voodoo Child,Hey Joe}");
	ASSERT_EQ(to_s(tr.obtenerClave("Eric Clapton")), "{Little Wing}");

  //Saco a Jimmy del Conjunto
	c.Eliminar("Jimmy Hendrix");
	tr.agregarTema("Bad Love", c);
  tr.agregarTema("Bell bottom blues",c);
	ASSERT_EQ(to_s(tr.obtenerClave("Eric Clapton")), "{Bell bottom blues,Bad Love,Little Wing}");
	ASSERT_EQ(to_s(tr.obtenerClave("Jimmy Hendrix")), "{Little Wing,Purple Haze,Voodoo Child,Hey Joe}");

	ASSERT_EQ(tr.estaDefinido("Hey Joe"),true);
	ASSERT_EQ(tr.estaDefinido("Voodoo Child"),true);
	ASSERT_EQ(tr.estaDefinido("Purple Haze"),true);
	ASSERT_EQ(tr.estaDefinido("Little Wing"),true);
	ASSERT_EQ(tr.estaDefinido("Bad Love"),true);

  //Saco Eric del Conjunto
  c.Eliminar("Eric Clapton");
  c.Agregar("Elvis Presley");
  tr.agregarAutor("Elvis Presley");
  tr.agregarTema("Always On My Mind", c);
  tr.agregarTema("Can't Help Falling In Love", c);
  tr.agregarTema("Hound dog", c);

  //Saco a Elvis del Conjunto
  c.Eliminar("Elvis Presley");
  c.Agregar("Elton John");
  tr.agregarAutor("Elton John");
  tr.agregarTema("Sacrifice", c);
  tr.agregarTema("I Guess That's Why They Call It The Blues",c);

  /*FTS:*/
  ASSERT_EQ(to_s(tr.FTS("Jimmy Hendrix", *(new Conj<string>) )) , "{Little Wing,Purple Haze,Voodoo Child,Hey Joe}");
  ASSERT_EQ(to_s(tr.FTS("Jimmy*", *(new Conj<string>) )) , "{Hey Joe,Voodoo Child,Purple Haze,Little Wing}");
  ASSERT_EQ(to_s(tr.FTS("Elton John", *(new Conj<string>) )) , "{I Guess That's Why They Call It The Blues,Sacrifice}")
  ASSERT_EQ(to_s(tr.FTS("Elton*", *(new Conj<string>) )) , "{Sacrifice,I Guess That's Why They Call It The Blues}")
  ASSERT_EQ(to_s(tr.FTS("Eric Clapton", *(new Conj<string>) )), "{Bell bottom blues,Bad Love,Little Wing}");
  ASSERT_EQ(to_s(tr.FTS("Eric*", *(new Conj<string>) )) , "{Little Wing,Bad Love,Bell bottom blues}");
  ASSERT_EQ(to_s(tr.FTS("Elvis*", *(new Conj<string>) )) , "{Always On My Mind,Can't Help Falling In Love,Hound dog}");
  //Elvis + Elton John:
  ASSERT_EQ(to_s(tr.FTS("El*", *(new Conj<string>) )),"{Always On My Mind,Can't Help Falling In Love,Hound dog,Sacrifice,I Guess That's Why They Call It The Blues}");
  //Eric Clapton + Elvis + Elton John:
  ASSERT_EQ(to_s(tr.FTS("E*", *(new Conj<string>) )) ,"{Little Wing,Bad Love,Bell bottom blues,Always On My Mind,Can't Help Falling In Love,Hound dog,Sacrifice,I Guess That's Why They Call It The Blues}" );
  //Todas Las Claves:
  ASSERT_EQ(to_s(tr.FTS("*", *(new Conj<string>) )) ,"{Voodoo Child,Purple Haze,Hey Joe,Little Wing,Hound dog,Sacrifice,I Guess That's Why They Call It The Blues,Can't Help Falling In Love,Bell bottom blues,Bad Love,Always On My Mind}" );

}



void test1(){
    Trie t;
    ASSERT_EQ(t.estaDefinido("Charly"),false);

    t.agregarAutor("Charly");
    ASSERT_EQ(t.obtenerClave("Charly").EsVacio(),true);
    Conj<string> c1;
    c1.Agregar("Charly");
    t.agregarTema("Los Dinosaurios",c1);
    t.agregarAutor("Charly Garcia");
    Conj<string> c2;
    c2.Agregar("Charly Garcia");
    t.agregarTema("Fanky",c2);
    Conj<string> res;
    t.FTS("Charl*",res);
    ASSERT_EQ(res.EsVacio(),false);
    ASSERT_EQ(res.Pertenece("Fanky"),true);
    ASSERT_EQ(res.Cardinal(),2);
    t.agregarAutor("Michael Mike");
    Conj<string> c3;
    c3.Agregar("Michael Mike");
    t.agregarTema("Charly Border",c3);
    Conj<string> res2;
    t.FTS("Charl*",res2);
    ASSERT_EQ(res2.Cardinal(),3);
    ASSERT_EQ(res2.Pertenece("Charly Border"),true);
    Conj<string> c4;
    c4 = t.FTS("Charly",c4);
    ASSERT_EQ(c4.EsVacio(),false);
}



int main() {


 

  RUN_TEST(check_trie_clona_autor);
  RUN_TEST(check_trie_claves);
  RUN_TEST(test1);

}
